export class WsConfigHandler {
  constructor(ipcMain, wsManager) {
    this.ipcMain = ipcMain
    this.wsManager = wsManager
    this.DEFAULT_ID = 1 // 默认配置记录ID

    this.registerHandlers()
  }

  // 注册所有 IPC 处理器
  registerHandlers() {
    this.ipcMain.handle('getWsConfig', () => this.getConfig())
    this.ipcMain.handle('updateWsConfig', (_, config) => this.updateConfig(config))
  }

  // 获取配置
  async getConfig() {
    try {
      const configs = await this.wsManager.getAll()
      const { host, port } = JSON.parse(configs)[0]
      return { success: true, data: { host, port } }
    } catch (error) {
      console.error('获取配置失败:', error)
      return { success: false, error: error.message }
    }
  }

  // 更新配置
  async updateConfig(config) {
    try {
      await this.wsManager.update(this.DEFAULT_ID, config.host, config.port)
      return { success: true }
    } catch (error) {
      console.error('更新配置失败:', error)
      return { success: false, error: error.message }
    }
  }
}
